Skip to content

Conversation

@Tehnix
Copy link
Contributor

@Tehnix Tehnix commented Oct 24, 2025

Problem

I've tried to follow the established conventions to enable wrapping the transcriptions API so that it can be used alongside the @posthog/ai sdk.

Relevant community thread in the PostHog docs https://posthog.com/questions/support-for-audio-transcriptions

Changes

  • Exposed new wrapper for the OpenAI transcriptions API
  • Tried to handle streaming as was done in other wrappers, selecting the relevant event type

Release info Sub-libraries affected

Libraries affected

  • All of them
  • posthog-js (web)
  • posthog-js-lite (web lite)
  • posthog-node
  • posthog-react-native
  • @posthog/react
  • @posthog/ai
  • @posthog/nextjs-config

Checklist

  • Tests for new code
  • Accounted for the impact of any changes across different platforms
  • Accounted for backwards compatibility of any changes (no breaking changes!)
  • Took care not to unnecessarily increase the bundle size

If releasing new changes

  • Ran pnpm changeset to generate a changeset file
  • Added the "release" label to the PR to indicate we're publishing new versions for the affected packages

@vercel
Copy link

vercel bot commented Oct 24, 2025

@Tehnix is attempting to deploy a commit to the PostHog Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +820 to +842
const wrappedPromise = parentPromise.then(
async (result) => {
if ('text' in result) {
const latency = (Date.now() - startTime) / 1000
await sendEventToPosthog({
client: this.phClient,
...posthogParams,
model: String(openAIParams.model ?? ''),
provider: 'openai',
input: openAIParams.prompt,
output: result.text,
latency,
baseURL: this.baseURL,
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0,
outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0,
},
})
return result
}
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: function returns undefined when result doesn't have text property (e.g., 'srt', 'vtt', 'text' response formats)

Suggested change
const wrappedPromise = parentPromise.then(
async (result) => {
if ('text' in result) {
const latency = (Date.now() - startTime) / 1000
await sendEventToPosthog({
client: this.phClient,
...posthogParams,
model: String(openAIParams.model ?? ''),
provider: 'openai',
input: openAIParams.prompt,
output: result.text,
latency,
baseURL: this.baseURL,
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0,
outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0,
},
})
return result
}
},
const wrappedPromise = parentPromise.then(
async (result) => {
if ('text' in result) {
const latency = (Date.now() - startTime) / 1000
await sendEventToPosthog({
client: this.phClient,
...posthogParams,
model: String(openAIParams.model ?? ''),
provider: 'openai',
input: openAIParams.prompt,
output: result.text,
latency,
baseURL: this.baseURL,
params: body,
httpStatus: 200,
usage: {
inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0,
outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0,
},
})
}
return result
},
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/openai/index.ts
Line: 820:842

Comment:
**logic:** function returns `undefined` when result doesn't have `text` property (e.g., 'srt', 'vtt', 'text' response formats)

```suggestion
      const wrappedPromise = parentPromise.then(
        async (result) => {
          if ('text' in result) {
            const latency = (Date.now() - startTime) / 1000
            await sendEventToPosthog({
              client: this.phClient,
              ...posthogParams,
              model: String(openAIParams.model ?? ''),
              provider: 'openai',
              input: openAIParams.prompt,
              output: result.text,
              latency,
              baseURL: this.baseURL,
              params: body,
              httpStatus: 200,
              usage: {
                inputTokens: result.usage?.type === 'tokens' ? (result.usage.input_tokens ?? 0) : 0,
                outputTokens: result.usage?.type === 'tokens' ? (result.usage.output_tokens ?? 0) : 0,
              },
            })
          }
          return result
        },
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +742 to +744
const parentPromise = openAIParams.stream
? super.create(openAIParams, options)
: super.create(openAIParams, options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: redundant ternary - both branches call the same function

Suggested change
const parentPromise = openAIParams.stream
? super.create(openAIParams, options)
: super.create(openAIParams, options)
const parentPromise = super.create(openAIParams, options)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/openai/index.ts
Line: 742:744

Comment:
**style:** redundant ternary - both branches call the same function

```suggestion
    const parentPromise = super.create(openAIParams, options)
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neccessary for TypeScript to figure out which overloaded super.create function to call, and removing this will result in a typescript error:

Argument of type 'TranscriptionCreateParams' is not assignable to parameter of type 'TranscriptionCreateParamsStreaming'.
      Type 'TranscriptionCreateParamsNonStreaming<AudioResponseFormat | undefined>' is not assignable to type 'TranscriptionCreateParamsStreaming'.
        Types of property 'stream' are incompatible.
          Type 'false | null | undefined' is not assignable to type 'true'.
            Type 'undefined' is not assignable to type 'true'.ts(2769)

@posthog-bot
Copy link
Collaborator

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

@Tehnix
Copy link
Contributor Author

Tehnix commented Nov 10, 2025

This is still very much relevant, I'm running a locally patched version of @posthog/ai currently that adds this.

@posthog-bot posthog-bot removed the stale label Nov 11, 2025
@davidsonsns
Copy link

@Tehnix maybe @daibhin or @andrewm4894 could help us

@daibhin daibhin requested a review from a team November 12, 2025 12:06
@daibhin
Copy link
Contributor

daibhin commented Nov 12, 2025

I'm afraid I know very little about the LLM stuff in our SDK but have tagged the @PostHog/team-llm-analytics team who are responsible for this

@andrewm4894 andrewm4894 self-assigned this Nov 12, 2025
@andrewm4894 andrewm4894 added team/llm-analytics LLM Analytics llmo This issue is related to LLM Observability. labels Nov 12, 2025
@andrewm4894
Copy link
Contributor

@Tehnix thanks for this! Looks great - I'll have a play around with it and try test it out locally.

# Conflicts:
#	packages/ai/tests/openai.test.ts
- Add language, response_format, and timestamp_granularities to getModelParams
- Ensures transcription-specific parameters are captured in $ai_model_parameters
- Fixes test failures for transcription language and response_format tests
@wiz-7ad640923b
Copy link

wiz-7ad640923b bot commented Nov 12, 2025

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities -
Data Finding Sensitive Data -
Total -

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try using Wiz Code VS Code Extension.

@andrewm4894 andrewm4894 changed the title Support OpenAI transcriptions via @posthog/ai feat(ai): Support OpenAI transcriptions via @posthog/ai Nov 12, 2025
@andrewm4894
Copy link
Contributor

@PostHog/team-llm-analytics tested this and all works great - added some tests too

Added changeset for the new OpenAI audio transcriptions API support in @posthog/ai package.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@andrewm4894
Copy link
Contributor

the ci errors here seem to be related to fact OP is not a member of the posthog org. have tested this and confirms works as expected - thanks @Tehnix for the contribution!!! 🙌

@andrewm4894 andrewm4894 merged commit aa9f637 into PostHog:main Nov 14, 2025
28 of 30 checks passed
@andrewm4894
Copy link
Contributor

@Tehnix thanks again for the contribution here - please email me at [email protected] if you want a voucher for some posthog merch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llmo This issue is related to LLM Observability. release team/llm-analytics LLM Analytics

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants